Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@types/csurf
Advanced tools
TypeScript definitions for csurf
@types/csurf provides TypeScript type definitions for the csurf middleware, which is used to protect against Cross-Site Request Forgery (CSRF) attacks in Node.js applications.
Basic CSRF Protection
This code demonstrates how to set up basic CSRF protection in an Express application using the csurf middleware. It includes setting up the middleware, generating a CSRF token, and using it in a form.
const express = require('express');
const csrf = require('csurf');
const cookieParser = require('cookie-parser');
const app = express();
const csrfProtection = csrf({ cookie: true });
app.use(cookieParser());
app.use(csrfProtection);
app.get('/form', (req, res) => {
res.send(`<!DOCTYPE html>
<html>
<body>
<form action="/process" method="POST">
<input type="hidden" name="_csrf" value="${req.csrfToken()}">
<button type="submit">Submit</button>
</form>
</body>
</html>`);
});
app.post('/process', (req, res) => {
res.send('Form processed');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
Custom Error Handling
This code demonstrates how to handle CSRF token errors by setting up custom error handling middleware. If a CSRF token error occurs, it sends a 403 status code and a custom error message.
const express = require('express');
const csrf = require('csurf');
const cookieParser = require('cookie-parser');
const app = express();
const csrfProtection = csrf({ cookie: true });
app.use(cookieParser());
app.use(csrfProtection);
app.use((err, req, res, next) => {
if (err.code !== 'EBADCSRFTOKEN') return next(err);
res.status(403);
res.send('Form tampered with');
});
app.get('/form', (req, res) => {
res.send(`<!DOCTYPE html>
<html>
<body>
<form action="/process" method="POST">
<input type="hidden" name="_csrf" value="${req.csrfToken()}">
<button type="submit">Submit</button>
</form>
</body>
</html>`);
});
app.post('/process', (req, res) => {
res.send('Form processed');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
Helmet is a middleware for Express applications that helps secure them by setting various HTTP headers. While it does not specifically handle CSRF protection, it provides a broader range of security features compared to csurf.
Express-rate-limit is a middleware for rate limiting in Express applications. It helps protect against brute-force attacks by limiting the number of requests a client can make. While it does not provide CSRF protection, it complements csurf by adding another layer of security.
Express-validator is a middleware for validating and sanitizing user input in Express applications. It helps prevent various types of attacks, including injection attacks. While it does not provide CSRF protection, it works well alongside csurf to enhance overall security.
npm install --save @types/csurf
This package contains type definitions for csurf (https://www.npmjs.org/package/csurf).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/csurf.
import express = require("express-serve-static-core");
declare global {
namespace Express {
interface Request {
csrfToken(): string;
}
}
}
declare function csurf(options?: {
value?: ((req: express.Request) => string) | undefined;
/**
* @default false
*/
cookie?: csurf.CookieOptions | boolean | undefined;
ignoreMethods?: string[] | undefined;
sessionKey?: string | undefined;
}): express.RequestHandler;
declare namespace csurf {
interface CookieOptions extends express.CookieOptions {
/**
* @default '_csrf'
*/
key?: string | undefined;
}
}
export = csurf;
These definitions were written by Hiroki Horiuchi, and Piotr Błażejewicz.
FAQs
TypeScript definitions for csurf
We found that @types/csurf demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.